require(["jquery","paging","template","bootstrap","echarts","bbcare","scprit/department/view/template/follow-up/common.js"],function($,Paging,template,bootstrap,echarts,$bb,$common) {
	var URL = $bb.baseUrl("department")+"followup/service";
	var obj=$bb.getPageParams();
	var userId=obj.babyId;
	var type=obj.type;//历史记录传参；
	'use strict';
	var count =1, pagesize =15;//历史记录分页；
	var HashMap=$common.hashmap(),hashMap = new HashMap();
	var btnSubmit= $("#submitData");
	$common.date();
	getBabyInfo();
	$(".mask, .AddInformation").hide();
	$("#historyData").click(function(event) {
		getBabyList(1, pagesize);
		var p = new Paging();
		$("#pageTool").html("");
		p.init({target:'#pageTool',pagesize:pagesize,count:count,toolbar:false,callback:function(page,size,count){
			getBabyList(page,size);
		}});
		$(".mask, .AddInformation").show();
	});
	$(".AddInformation h4 span").click(function(event) {
		$(".mask, .AddInformation").hide();
	});
	var account=eval('('+sessionStorage.account+')');
	var accountName=account.alias;
	$("#userName").html(accountName);//检查医生名字
	$("#nowTime").html($common.getNowFormatDate());
	function getBabyInfo() {
		$bb.ajax({
			url: URL+"/patient/patientInfoPtId",
			data: JSON.stringify({
				data: {
					"patientId":userId
				}
			}),
			success: function (data) {
				var babyInfo=data.data.PatientInfo;
				if(data.success) {
					var babyData=$common.babyInfoDeal(babyInfo);
					$common.infoInit({"id":"#babyInfoList","data":babyData});
				} else {
					$bb.toastShow("患者信息获取失败！",1500);
				}
			}
		});
	}
	$(".td-chd ul li input").click(function () {
		var res = countScore();
		$("#showSubjects").html(res.scores + "分+"+res.not + "N/A");
	});
	btnSubmit.click(function(){
		HashMap.prototype.constructor = HashMap;
		var ability = 0, passiveMuscle = 0, activeMuscles = 0, originalReflection = 0, generalResponse = 0;
		var hashMap = new HashMap();
		$(".td-chd ul :checked").each(function () {
			var id = $(this).attr("name");
			var val = $(this).val() == "N/A" ? '0':$(this).val();
			if(id=='1'||id=='2'||id=='3'||id=='4'||id=='5'||id=='6') ability+=val*1.0;
			if(id=='7'||id=='8'||id=='9'||id=='10') 				 passiveMuscle+=val*1.0;
			if(id=='11'||id=='12'||id=='13'||id=='14') 				 activeMuscles+=val*1.0;
			if(id=='15'||id=='16'||id=='17') 						 originalReflection+=val*1.0;
			if(id=='18'||id=='19'||id=='20') 						 generalResponse+=val*1.0;
		});
		hashMap.put("ability", ability);
		hashMap.put("passiveMuscle", passiveMuscle);
		hashMap.put("activeMuscles", activeMuscles);
		hashMap.put("originalReflection", originalReflection);
		hashMap.put("generalResponse", generalResponse);
		//获取其name和值：
		var nameValMap = countSubjects();

		$bb.ajax({
			url: URL+"/patient/evaluation/getScore",
			data: JSON.stringify({
				data: {
					"userId" : userId,
					"examinationPaperName" : type,
					"postionIds" : JSON.stringify(nameValMap),
					"postionScores":JSON.stringify(hashMap)
				}
			}),
			success: function (data) {
				if(data.success) {
					localStorage.setItem("nbna",JSON.stringify(data.data));
					$bb.openPage("nbnaResult",obj);
				} else {
					alert("获取数据失败！");
				}
			}
		});
	});
	//历史记录列表点击：
	$("#dataList").on('click',"ul[result]",function () {
		var data = eval("("+hashMap.get($(this).attr("result")).replace(/\=/g,"\:")+")");
		localStorage.setItem("nbna",JSON.stringify(data));
		$bb.openPage("nbnaResult",obj);
	});
	//历史记录列表：
	function getBabyList(page, rows) {
		$bb.ajax({
			url: URL+"/patient/evaluation/getHistoryScore",//baby地址
			async: false,
			data: JSON.stringify({
				data: {
					"userId": userId,
					"examinationPaperName": type,
					"page": page,
					"rows": rows
				}
			}),
			success: function (data) {
				if (data.success) {
					hashMap = new HashMap();
					count = data.total;
					var babyData=data.rows;
					if(count==0){
						$bb.toastShow("患者还未参与该项测评！");
					}
					var str = '';
					for (var i = 0; i < data.rows.length; i++) {
						hashMap.put(data.rows[i].answerId, data.rows[i].result);
						var pretermBirth = 37 * 7 > data.rows[i].pregnancyWeek ? "是" : "否";
						var nowDate =$common.getNowFormatDate();
						var monthAge = $common.DateDiff(nowDate, data.rows[i].birthday) / 30;
						var weight= data.rows[i].birthWeight==undefined?"未知":data.rows[i].birthWeight+"(g)";
						var doctorName= data.rows[i].doctorName==undefined?"未知":data.rows[i].doctorName;

						str += '<ul class="clearfix" result="' + data.rows[i].answerId + '">';
						str += '<li>' + data.rows[i].babyName + '</li>';
						str += '<li>' + weight+ '</li>';
						str += '<li>' + data.rows[i].birthday + '</li>';
						str += '<li>' + data.rows[i].pregnancyWeek + '(天)</li>';
						str += '<li>' + pretermBirth + '</li>';
						str += '<li>' + doctorName + '</li>';
						str += '<li>' + data.rows[i].createTime + '</li>';
						str += '</ul>';
					}
					$("#dataList").html(str);
				}
			}
		});
	}
	var countSubjects = function(){
		var map = new HashMap();
		$(".td-chd ul :checked").each(function () {
			var name = $(this).attr("name");
			var nameVal=$(this).val();
			map.put(name,nameVal);
		});
		return map
	};
	var countScore = function(){
		var subject = countSubjects().map;
		var notDoLens = 0,scores = 0 ;
		for(var item in subject){
			if(subject[item] == "N/A"){
				notDoLens++;
			}else{
				scores += subject[item]*1;
			}
		}
		return {
			"not":notDoLens,
			"scores":scores
		}
	}
});